package com.aidex.biz.service.impl;

import com.aidex.biz.domain.Order;
import com.aidex.biz.domain.ProductSku;
import com.aidex.biz.service.EquInfoService;
import com.aidex.biz.service.ProductSkuService;
import com.aidex.biz.service.QingYangOrderService;
import com.aidex.biz.utils.AddressUtil;
import com.aidex.biz.utils.HttpUtilsByAliyun;
import com.aidex.biz.utils.SignUtilsThird;
import com.aidex.system.service.CompanyService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信商户支付信息Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-09-21
 */
@Service
public class QingYangOrderServiceImpl implements QingYangOrderService {
    private static final Logger log = LoggerFactory.getLogger(QingYangOrderServiceImpl.class);
    @Autowired
    private EquInfoService equInfoService;
    @Autowired
    private CompanyService companyService;

    @Autowired
    private ProductSkuService productSkuService;

    private static  final String apiKey="KQlZdvjZ2wfysYCu";
    private static  final String apiSecret="oP9tz7c6yIao7gCu";

    private  static  final String platformKey="urgeka";

    private static  final String qingYangUrl="https://o2o-third-api.funxi.cn/third/v1/order/";
    /**
     * "platformKey": "test",
     *     "platformSkuId": "1",
     *     "platformOrderNumber": "third_order_1",
     *     "comment": "12345",
     *     "beginTime": "2023-09-09 11:11:11",
     *     "orderUserName": "测试用户",
     *     "orderUserPhone": "13800000001",
     *     "name": "测试用户",
     *     "phone": "13800000001",
     *     "province": "北京",
     *     "city": "北京",
     *     "area": "海淀区",
     *     "street": "xxxx路",
     *     "building": "yyy小区",
     *     "detail": "zzzz单元101"
     * @param order
     * @return
     */
    @Override
    public JSONObject pushQingYangOrder(Order order) throws Exception {
        Map<String,String > header=new HashMap<>();
        JSONObject body=new JSONObject();
        // 添加头信息
        header.put("APIKEY",apiKey);
        header.put("TIMESTAMP",System.currentTimeMillis()/1000+"");
        JSONObject result=new JSONObject();
        body.put("platformKey",platformKey);
        if(order.getOrderDetails().size()>0){
            ProductSku productSku=productSkuService.get(order.getOrderDetails().get(0).getProductSkuId());
            body.put("platformSkuId",productSku.getQySkuId());
        }else{
            body.put("platformSkuId","1");
        }
        body.put("platformOrderNumber",order.getOrderId());
        body.put("comment",order.getRemark());
        body.put("beginTime",header.get("TIMESTAMP"));
        // 上门取件   收件人是下单人的地址
        //if("1".equals(order.getOrderInType())){
            body.put("orderUserName",order.getLianxiName());
            body.put("orderUserPhone",order.getPhone());
            body.put("name",order.getLianxiName());
            body.put("phone",order.getPhone());
            JSONObject adJson= AddressUtil.geocoder(order.getRoomAddress());
            body.put("province",adJson.get("province"));
            body.put("city",adJson.get("city"));
            body.put("area",adJson.get("district"));
            body.put("street",adJson.get("street"));
            if(null==adJson.get("street_number")){
                body.put("detail",order.getRoomAddress());
            }else{
                body.put("detail",adJson.get("street_number"));
            }
//        }else{
//            // 自行投递 获取驿站信息
//            EquInfo equInfo=equInfoService.get(order.getCompanyId());
//            Company company=companyService.get(order.getCompanyId());
//            body.put("orderUserName",equInfo.getEquXinghao()+"/"+order.getLianxiName());
//            body.put("orderUserPhone",order.getPhone());
//            body.put("name",order.getLianxiName());
//            body.put("phone",order.getPhone());
//            JSONObject adJson= AddressUtil.geocoder(equInfo.getEquAddress());
//            body.put("province",adJson.get("province"));
//            body.put("city",adJson.get("city"));
//            body.put("area",adJson.get("district"));
//            body.put("street",adJson.get("street"));
//            if(null==adJson.get("detail")){
//                body.put("detail",company.getLianxiAddress());
//            }else{
//                body.put("detail",adJson.get("detail"));
//            }
//        }
        log.error("推送订单信息："+body.toJSONString());
        header.put("SIGN", SignUtilsThird.generateSign(body.toString(),header.get("APIKEY"),header.get("TIMESTAMP"),
                apiSecret));
        try {
            HttpResponse response=HttpUtilsByAliyun.doPost(qingYangUrl+"/create",header,body);
            result = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
            result.put("error", "连接错误！");
        }
        return result;
    }

    @Override
    public JSONObject modifyDeliveryNumber(Order order) throws Exception {
        Map<String,String > header=new HashMap<>();
        JSONObject body=new JSONObject();
        header.put("APIKEY",apiKey);
        header.put("TIMESTAMP",System.currentTimeMillis()/1000+"");
        JSONObject result=new JSONObject();
        body.put("platformKey",platformKey);
        body.put("platformOrderNumber",order.getOrderId());
        body.put("deliveryNumber",order.getDeliveryNo());
        // 签名
        header.put("SIGN", SignUtilsThird.generateSign(body.toString(),header.get("APIKEY"),header.get("TIMESTAMP"),
                apiSecret));
        HttpResponse response=null;
        try {
            response =HttpUtilsByAliyun.doPut("https://o2o-third-api.funxi.cn/","third/v1/order" +
                    "/modifyDeliveryNumber",header,null,body.toString());
        } catch (Exception e) {
            e.printStackTrace();
            result.put("error", "连接错误！");
        }
        return JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
    }

    public static void main(String[] args) {
        String url=qingYangUrl;

        Map<String,String > header=new HashMap<>();

       JSONObject body=new JSONObject();
        // 添加头信息
        header.put("APIKEY",apiKey);
        header.put("TIMESTAMP",System.currentTimeMillis()/1000+"");
        JSONObject result=new JSONObject();
        body.put("platformKey",platformKey);
        body.put("platformOrderNumber","20231029172742556");
        body.put("deliveryNumber","JDVE09303986852");

        header.put("SIGN", SignUtilsThird.generateSign(body.toString(),header.get("APIKEY"),header.get("TIMESTAMP"),
                apiSecret));
        System.out.println(body.toString());
        try {
            HttpResponse response=HttpUtilsByAliyun.doPut("https://o2o-third-api.funxi.cn/","third/v1/order" +
                    "/modifyDeliveryNumber",header,null,body.toString());
//            if (response.getStatusLine().getStatusCode() == 200) {
                result = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
//            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put("error", "连接错误！");
        }
        System.out.println(result);
    }




}
